<!DOCTYPE html>
<html>
<head>   
    <title>instances</title>   
    <script type="text/javascript" src="../javascript/page/Child.js"></script>
    <style type="text/css">
        html, body {
            font-size: 12px;  background-color: #f1f1f1;
        }
    </style>
</head>
<body onload="docLoaded();">
    <div id="queryForm" class="title_bar">
        <template>
            <div class="title_bar">
                <el-form :inline="true" :model="searchData" class="demo-form-inline">           
                    <el-form-item label="Application">
                        <el-input v-model="searchData.name" placeholder="Application name"></el-input>
                    </el-form-item>    
                    <el-form-item label="Status">
                        <el-select v-model="searchData.status" multiple placeholder="please select status">
                            <el-option v-for="item in statusList"
                            :key="item.status"
                            :label="item.name"
                            :value="item.status">
                            </el-option>
                        </el-select>
                    </el-form-item>          
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">Query</el-button>
                    </el-form-item>                   
                </el-form>
            </div>       
            <div class="hline"></div>
            <div class="panel">              
                <el-table :data="tableData" style="width: 100%" max-height="560" stripe= "true">
                    <el-table-column prop="id" label="ID" min-width="5%"></el-table-column>
                    <el-table-column prop="name" label="Application" min-width="10%"></el-table-column>
                    <el-table-column prop="managementUrl" label="ManagementUrl" min-width="15%"></el-table-column>
                    <el-table-column prop="healthUrl" label="HealthUrl" min-width="15%"></el-table-column>
                    <el-table-column prop="serviceUrl" label="ServiceUrl" min-width="10%"></el-table-column>
                    <el-table-column prop="heartbeatTime" label="HeartbeatTime" min-width="10%"></el-table-column>
                    <el-table-column prop="statusName" label="Status" min-width="10%"></el-table-column>
                    <el-table-column label="Operation" min-width="25%">
                        <template slot-scope="scope">
                            <el-link type="primary" @click="handleRefresh(scope.row)" underline="false">刷新</el-link>  
                            <el-link type="primary" :href="'./profiles.htm?appId='+scope.row.appId+'&appName='+scope.row.name" underline="false">详情</el-link>                          
                        </template>
                    </el-table-column>
                </el-table>                 
            </div>   
            <div class="status_bar" style="text-align: left;">
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="searchData.pageNum"
                    :page-sizes="[10, 15, 20]"
                    :page-size="searchData.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="pageInfo.total">
                </el-pagination> 
            </div>            
        </template>
    </div>
    <script type="text/javascript">
        var router = startPage(); 
        function docLoaded() {
            var queryApp = new Vue({el: '#queryForm', data: {
                    searchData:{
                        name:"",
                        status: [],
                        pageNum:1,
                        pageSize:10                       
                    },
                    pageInfo:{
                        totalPage:0,
                        total:0
                    },
                    tableData:[],
                    statusList:[{status:0,name:"start"},{status:1,name:"live"},{status:2,name:"pause"},{status:3,name:"stop"}],
                    formLabelWidth: '120px'                 
                },
                methods: {
                    onSubmit:function(event){                                           
                        httpPost(apiInstancePage(this,router.authInfo,JSON.stringify(this.searchData))).success(function(res,context){ 
                            parseAndCheckResult(context,res);
                        }).then(function(status, res, context){context.$message.error(res);});
                    },
                    handleSizeChange(val) {                     
                        this.searchData.pageSize = val;
                        this.onSubmit();
                    },
                    handleCurrentChange(val) {
                        this.searchData.pageNum = val;
                        this.onSubmit();
                    },
                    handleClick(row){
                        console.log(row);
                    },
                    handleRefresh:function(row){         
                        var reqData = {id:row.id,appId:row.appId};               
                        var apiRequest = apiInstanceRefresh(this,router.authInfo,JSON.stringify(reqData));
                        httpPost(apiRequest).success(function(res,context){                          
                            checkResult(context,res,false);
                        }).then(function(status, res, context){checkResult(context,res,true);});
                    }
                }
            }); queryApp.onSubmit();//init end.
        } 

        function parseAndCheckResult(vApp, res){
            var resObj = JSON.parse(res); 
            if(resObj.code != SUCCESS_CODE){
                return false;
            }
            if(resObj.pageInfo){
                vApp.pageInfo.total = resObj.pageInfo.total;
                vApp.pageInfo.totalPage = resObj.pageInfo.totalPage;
            }
            if(resObj.data){
                var arrayObj = new Array();
                for(var item of resObj.data){
                    arrayObj.push({
                        id:item.id,
                        name:item.name, 
                        appId: item.appId,
                        managementUrl: item.managementUrl,
                        healthUrl:item.healthUrl, 
                        serviceUrl:item.serviceUrl, 
                        heartbeatTime: dateFmt("yyyy-MM-dd HH:mm:ss", new Date(item.heartbeatTime)),
                        serviceInfo: item.serviceInfo,
                        currentStatus: item.currentStatus,                       
                        statusName:getStatusName(item.currentStatus)});
                }
                vApp.tableData = arrayObj;
            }
            return resObj;
        }

        function getStatusName(status){
            switch(status){
                case 0:return "start";
                case 1:return "live";
                case 2:return "pause";
                default:return "stop";
            }    
        }

        function checkResult(vApp, res, isErr){            
            if(isErr){
                vApp.$message.error(res);
                return;
            }
            var resObj = JSON.parse(res); 
            if(resObj.code == SUCCESS_CODE){
                vApp.$message({type: 'success', message: resObj.message});
                return;
            }
            vApp.$message.error(resObj.message+","+resObj.code);
        }
    </script>
</body>
</html>
